package servlet;

import DAO.SubCategoryDao;
import bean.SubCategory;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

@WebServlet("/editSubCategory")
public class EditSubCategoryServlet extends HttpServlet {
    private SubCategoryDao subCategoryDao = new SubCategoryDao();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        String action = request.getParameter("action");
        
        if ("showEditForm".equals(action)) {
            // 获取大分类ID
            int categoryId = Integer.parseInt(request.getParameter("categoryId"));
            
            // 获取该大分类下的所有小分类
            List<SubCategory> subCategories = subCategoryDao.getSubCategoriesByCategoryId(categoryId);
            
            // 将小分类列表存入request
            request.setAttribute("subCategories", subCategories);
            
            // 转发到编辑页面
            request.getRequestDispatcher("edit_subcategory.jsp").forward(request, response);
        } else if ("getSubCategoryDetails".equals(action)) {
            // 获取小分类详情的AJAX请求
            int subCategoryId = Integer.parseInt(request.getParameter("subCategoryId"));
            SubCategory subCategory = subCategoryDao.getSubCategoryById(subCategoryId);
            
            // 手动构建JSON响应
            String jsonResponse = String.format(
                "{\"id\":%d,\"name\":\"%s\",\"description\":\"%s\",\"categoryId\":%d}",
                subCategory.getId(),
                subCategory.getName(),
                subCategory.getDescription() != null ? subCategory.getDescription() : "",
                subCategory.getCategoryId()
            );
            
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(jsonResponse);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        String action = request.getParameter("action");
        
        if ("updateSubCategory".equals(action)) {
            try {
                // 获取表单数据
                int subCategoryId = Integer.parseInt(request.getParameter("subCategoryId"));
                String name = request.getParameter("name");
                String description = request.getParameter("description");
                
                // 创建SubCategory对象
                SubCategory subCategory = new SubCategory();
                subCategory.setId(subCategoryId);
                subCategory.setName(name);
                subCategory.setDescription(description);
                
                // 更新小分类
                subCategoryDao.updateSubCategory(subCategory);
                
                // 更新成功后重定向到分类列表页面
                response.sendRedirect(request.getContextPath() + "/category");
            } catch (Exception e) {
                e.printStackTrace();
                request.setAttribute("error", "更新小分类失败：" + e.getMessage());
                doGet(request, response); // 重新显示表单
            }
        }
    }
} 